<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <!--Inicia la composicion de la pagina en la plantilla-->
    <ui:composition template="/resource/template/plantilla.xhtml">

        <!--Segmento que define el contenido del header-->
        <ui:define name="head">
            <h:head>
                <f:loadBundle basename="person.location.label_#{logged.loggedIdiom}" var="label" />
                <f:loadBundle basename="person.location.message_#{logged.loggedIdiom}" var="message" />
                <f:loadBundle basename="person.location.help_#{logged.loggedIdiom}" var="help" />

                <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"/>
                <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                <script type="text/javascript" src="../../resource/js/jquery-1.7.2.min.js"/>
                <script type="text/javascript" src="../../resource/js/jquery-ui-1.8.21.custom.min.js"/>

                <style type="text/css">
                    html { height: 100% }
                    body { height: 100%; margin: 0; padding: 0 }
                    #map_canvas { height: 100% }
                </style>

                <script type="text/javascript">
                    var marker;
                    var map;
                    var vzoom = 15;

                    function initialize() {
                        var myLatlngInit = new google.maps.LatLng(8.932509021104444, -79.8275537603455);
                        var myLatlng = new google.maps.LatLng(#{person_location.lat}, #{person_location.lng});

                        if (myLatlng.toString() == myLatlngInit.toString()) {
                            vzoom = 7;
                        }

                        var myOptions = {
                            center: myLatlng,
                            mapTypeId: google.maps.MapTypeId.ROADMAP,
                            zoom: vzoom
                        };
                        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

                        marker = new google.maps.Marker({position: myLatlng, map: map});

                        google.maps.event.addListener(map, 'click', function(event) {
                            if (marker) {
                                marker.setPosition(event.latLng);
                            } else {
                                marker = new google.maps.Marker({
                                    position: event.latLng,
                                    map: map
                                });
                            }
                            $('#form1\\:lat').val(event.latLng.lat());
                            $('#form1\\:lng').val(event.latLng.lng());
                        });
                    }
                </script>   
                <script>
                    function verificar(dato)
                    {
                        var agree = confirm(dato);
                        if (agree)
                            return true;
                        else
                            return false;
                    }
                </script>
            </h:head>
        </ui:define>

        <!--Segmento que define el contenido de la pagina-->
        <ui:define name="content">
            <h:outputText value="#{message.m0}" class="label" rendered="#{!access.visit}"/>
            <h:form id="form1" rendered="#{access.visit}">  
                <p:growl id="messages" showDetail="true"/>  
                <h:panelGroup>
                    <!--TIPO-->
                    <div class="field">
                        <h:outputText value="#{label.field1}" class="label"/>
                        <h:selectOneMenu id="tipo" class="select" value="#{person_location.idl_tipo}" required="#{param['required']=='true'}" requiredMessage="#{message.m22}">  
                            <f:selectItem itemLabel="" itemValue=""/> 
                            <f:selectItem itemLabel="#{label.tipo1}" itemValue="1"/> 
                            <f:selectItem itemLabel="#{label.tipo2}" itemValue="2"/> 
                            <f:selectItem itemLabel="#{label.tipo3}" itemValue="3"/> 
                        </h:selectOneMenu>
                    </div>

                    <!--PAIS-->
                    <div class="field">
                        <h:outputText value="#{label.field2}" class="label"/>
                        <h:selectOneMenu id="pais" value="#{person_location.countryBeans.id}" valueChangeListener="#{person_location.selectedCountry}" onchange="submit()"
                                         onselect="vpais()" required="#{param['required']=='true'}" requiredMessage="#{message.m23}">
                            <f:selectItem itemLabel="" itemValue=""/>
                            <f:selectItems value="#{location_countryFind.listItem()}"/>  
                        </h:selectOneMenu>
                        <h:commandButton id="btnPais" class="addButton" onclick="addPais.show();" type="button"/>
                        <!--Dialogos para la insertcion de pais-->
                        <p:dialog id="addPais" widgetVar="addPais" header="#{label.title2}" modal="true" resizable="false">
                            <h:inputText value="#{person_location.countryBeans.nombre}" class="textfield"/>
                            <h:commandButton value="#{label.button1}" class="button" actionListener="#{person_location.insertCountry}" onclick="addPais.hide();"/>
                        </p:dialog>
                    </div>

                    <div class="field">
                        <!--PROVINCIA-->
                        <h:outputText value="#{label.field3}" class="label"/>
                        <h:selectOneMenu id="provincia" value="#{person_location.provinceBeans.id}" valueChangeListener="#{person_location.selectedProvince}" onchange="submit()"
                                         required="#{param['required']=='true'}" requiredMessage="#{message.m24}">
                            <f:selectItem itemLabel="" itemValue=""/>
                            <f:selectItems value="#{location_provinceFind.listItem(person_location.countryBeans.id)}" /> 
                        </h:selectOneMenu>
                        <h:commandButton class="addButton" onclick="addProvincia.show();" type="button"/>
                        <!--Dialogos para la insertcion de provincia-->
                        <p:dialog id="addProvincia" widgetVar="addProvincia" header="#{label.title3}" modal="true" resizable="false">
                            <h:inputText value="#{person_location.provinceBeans.nombre}" class="textfield" />
                            <h:commandButton value="#{label.button1}" class="button" actionListener="#{person_location.insertProvince}" onclick="addProvincia.hide();"/>
                        </p:dialog>
                    </div>

                    <!--DISTRITO-->
                    <div class="field">
                        <h:outputText value="#{label.field4}" class="label" />
                        <h:selectOneMenu id="distrito" value="#{person_location.districtBeans.id}" valueChangeListener="#{person_location.selectedDistric}" onchange="submit()" 
                                         required="#{param['required']=='true'}" requiredMessage="#{message.m25}">
                            <f:selectItem itemLabel="" itemValue=""/>
                            <f:selectItems value="#{location_districtFind.listItem(person_location.provinceBeans.id)}" /> 
                        </h:selectOneMenu>
                        <h:commandButton class="addButton" onclick="addDistrito.show();" type="button"/>
                        <!--Dialogos para la insertcion de distrito-->
                        <p:dialog id="addDistrito" widgetVar="addDistrito" header="#{label.title4}" modal="true" resizable="false">
                            <h:inputText value="#{person_location.districtBeans.nombre}" class="textfield" />
                            <h:commandButton value="#{label.button1}" class="button" actionListener="#{person_location.insertDistrict}" onclick="addDistrito.hide();"/>
                        </p:dialog>
                    </div>

                    <!--CORREGIMIENTO-->
                    <div class="field">
                        <h:outputText value="#{label.field5}" class="label" />
                        <h:selectOneMenu id="corregimiento" value="#{person_location.id_Corregimiento}" valueChangeListener="#{person_location.selectedTownship}" onchange="submit()"
                                         required="#{param['required']=='true'}" requiredMessage="#{message.m26}">
                            <f:selectItem itemLabel="" itemValue=""/>
                            <f:selectItems value="#{location_townshipFind.listItem(person_location.districtBeans.id)}" /> 
                        </h:selectOneMenu>
                        <h:commandButton class="addButton" onclick="addCorregimiento.show();" type="button"/>
                        <!--Dialogos para la insertcion de corrregimiento-->
                        <p:dialog id="addCorregimiento" widgetVar="addCorregimiento" header="#{label.title5}" modal="true" resizable="false">
                            <h:inputText value="#{person_location.townshipBeans.nombre}" class="textfield" />
                            <h:commandButton value="#{label.button1}" class="button" actionListener="#{person_location.insertTownship}" onclick="addCorregimiento.hide();"/>
                        </p:dialog>
                    </div>

                    <!--DIRECCION-->
                    <div class="field">
                        <h:outputText value="#{label.field6}" class="label"/>
                        <h:commandButton  onclick="mapa.show();" class="mapButton" type="button"/>
                        <h:inputTextarea id="direccion" value="#{person_location.direccion}" class="textArea"/>
                    </div>
                    <!--Dialogo del Mapa de Google -->
                    <p:dialog onShow="initialize()" id="mapa" widgetVar="mapa" modal="true" header="Google Maps" height="500" width="500">
                        <div id="map_canvas" style="width:100%; height:100%"></div>
                    </p:dialog>
                    <h:inputHidden id="lat" value="#{person_location.lat}"/>
                    <h:inputHidden id="lng" value="#{person_location.lng}"/>

                    <!--INSERTAR-->
                    <h:commandButton value="#{label.button1}" class="button" action="#{person_location.insert}" rendered="#{access.add}" onclick="return verificar('#{message.m7}')">
                        <f:param name="required" value="true"/>
                    </h:commandButton>
                    <!--ACTUALIZAR-->
                    <h:commandButton value="#{label.button2}" class="button" action="#{person_location.update}" rendered="#{access.update}" onclick="return verificar('#{message.m8}')">
                        <f:param name="required" value="true"/>
                    </h:commandButton>
                    <!--ELIMINAR-->
                    <h:commandButton value="#{label.button3}" class="button" action="#{person_location.delete}" rendered="#{access.delete}" onclick="return verificar('#{message.m9}')"/>
                    <!--LIMPIAR-->
                    <h:commandButton value="#{label.button4}" class="button" action="#{person_location.clear}" rendered="#{access.visit}"/>
                </h:panelGroup>
            </h:form>
            <h:form>
                <c:set var="list" value="#{person_locationFind.listByLoggedId}"/>
                <h:panelGroup layout="block" class="tableWrapper" rendered="#{not empty list}">
                    <h:dataTable var="p" value="#{list}" class="dataTable">
                        <h:column >
                            <h:commandLink styleClass="editButton" rendered="#{access.update}" actionListener="#{person_location.selectedTempId(p.id)}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">#{label.field1}</f:facet>
                            <c:set var="tipo" value="tipo#{p.idl_tipo}" />
                            <h:outputText value="#{label[tipo]}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">#{label.field6}</f:facet>
                            <h:outputText value="#{p.direccion}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">#{label.field2}</f:facet>
                            <h:outputText value="#{p.countryBeans.nombre}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">#{label.field3}</f:facet>
                            <h:outputText value="#{p.provinceBeans.nombre}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">#{label.field4}</f:facet>
                            <h:outputText value="#{p.districtBeans.nombre}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">#{label.field5}</f:facet>
                            <h:outputText value="#{p.townshipBeans.nombre}"/>
                        </h:column>
                    </h:dataTable>
                </h:panelGroup>
            </h:form>
        </ui:define>

        <!--Segemento donde se define un texto de ayuda para que el usuario pueda utilizar el formulario del contenido-->
        <ui:define name="slideHelper">
            <h3>#{help.title1}</h3>
            #{help.detail1a}
            <p>
                #{help.title2}
                <ul>
                    <li>#{help.detail2a}</li>
                    <li>#{help.detail2b}</li>
                    <li>#{help.detail2c}</li>
                </ul>
            </p>
            <p>
                #{help.title3}
                <ul>
                    <li>#{help.detail3a}</li>
                    <li>#{help.detail3b}</li>
                </ul>
            </p> 
        </ui:define>
    </ui:composition>
</html>
